package com.trust;

import org.apache.commons.logging.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@SpringBootApplication
public class MybabyApplication extends SpringBootServletInitializer {
    private static Log logger = LogFactory.getLog(MybabyApplication.class);

	@Bean
	protected ServletContextListener listener() {
		return new ServletContextListener() {
			@Override
			public void contextInitialized(ServletContextEvent sce) {
				logger.info("初始化ServletContext initialized");
				WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext())
						.getAutowireCapableBeanFactory().autowireBean(this);
				// 设置系统名称
				ServletContext servletContext = sce.getServletContext();
				servletContext.setAttribute("app", servletContext.getContextPath());
			}
			@Override
			public void contextDestroyed(ServletContextEvent sce) {
				logger.info("ServletContext destroyed");
			}

		};
	}
	@Bean
	public CharacterEncodingFilter characterEncodingFilter() {
		CharacterEncodingFilter filter = new CharacterEncodingFilter();
		filter.setEncoding("UTF-8");
		filter.setForceEncoding(true);
		return filter;
	}
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		// TODO Auto-generated method stub
		return builder.sources(MybabyApplication.class);
	}

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(MybabyApplication.class);
		app.run(args);
	}

}
